﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.UserManage.route
{
    /// <summary>
    /// 内部报价设置
    /// </summary>
    public partial class seftroutegrid : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = "";

            if (ViewState["condition"] == null || ViewState["condition"].ToString() == "")
            {
                strWhere = "routeid = 0";
            }
            else
            {
                strWhere = "routeid = 0 AND " + ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = RouteBases.GetRouteBasesCount(strWhere);
            DataGrid1.DataSource = RouteBases.GetRouteBaseTableByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "", "");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            string loadstr = loadport.Text;
            string deststr = destport.Text;
            string routestr = SASRequest.GetString("ajaxroutelinedatalist_value");
            string boatstr = SASRequest.GetString("ajaxcarrierlist_value");
            ViewState["condition"] = RouteBases.GetRouteBaseCondation(companyinfo.id, -1, loadstr, deststr, routestr, boatstr);
            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        protected void setdel_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                RouteBases.DelRouteBase(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 快捷修改
        /// </summary>
        protected void EditAll_Click(object sender, EventArgs e)
        {
            int row = 0;
            foreach (object o in DataGrid1.GetKeyIDArray())
            {
                int id = int.Parse(o.ToString());
                string price1 = DataGrid1.GetControlValue(row, "20GP").Trim();
                string price2 = DataGrid1.GetControlValue(row, "40GP").Trim();
                string price3 = DataGrid1.GetControlValue(row, "40HQ").Trim();

                if (RouteBases.UpdateRouteBasePrice(id, (TypeConverter.StrToFloat(price1, 0)).ToString(), (TypeConverter.StrToFloat(price2, 0)).ToString(), (TypeConverter.StrToFloat(price3, 0)).ToString()) <= 0)
                {
                    break;
                }
                OpLogs.LogRouteEdit(userid, username, id, price1, price2, price3, "快捷修改");
                row++;
            }
            base.RegisterStartupScript("PAGETEMPLATE", "价格修改成功！");
            BindDataGrid();
        }

        /// <summary>
        /// 其他价格生成
        /// </summary>
        protected string GetOtherPrice(string price, int ordernum)
        {
            string[] pricearray = price.Split(',');

            if (pricearray.Length < 3) return Utils.StrToInt(price, 0).ToString();
            else return pricearray[ordernum - 1] == "" ? "0" : pricearray[ordernum - 1];
        }

        /// <summary>
        /// 过期信息查看
        /// </summary>
        protected void passtime_Click(object sender, EventArgs e)
        {
            ViewState["condition"] = RouteBases.GetRouteBaseCondation(companyinfo.id, -1, "", "", "", "") + " AND validend < GETDATE() ";
            BindDataGrid();
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            ViewState["condition"] = RouteBases.GetRouteBaseCondation(companyinfo.id, -1, "", "", "", "");

            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = "线路基本信息管理";
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            setdel.Click += new EventHandler(setdel_Click);
            EditAll.Click += new EventHandler(EditAll_Click);
            passtime.Click += new EventHandler(passtime_Click);

            if (!OpAction(OprateAction.Delete)) setdel.Visible = false;
            if (!OpAction(OprateAction.Update)) { setprice.Visible = false; setinfo.Visible = false; }
            if (!OpAction(OprateAction.Create)) add.Visible = false;
        }

        #endregion
    }
}